package org.client.chat;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.Socket;

import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.model.ChatStrings;
import org.secure.SecureInputStream;
import org.secure.SecureOutputStream;

import sun.audio.AudioPlayer;

public class ITalkServer extends ITalkChatWindow implements DisposeListener{

	private SecureOutputStream outputStream;
	private SecureInputStream inputStream;
	private Socket currentSocket;
	private String friendName;
	private boolean isRecieving = false;
	private String loginName;
	
	public ITalkServer(Socket tempSocket,String loginName, byte[] key)
	{
		this.loginName = loginName;
		try {
			
			outputStream = new SecureOutputStream(tempSocket.getOutputStream());
			inputStream = new SecureInputStream(new InputStreamReader(tempSocket.getInputStream()));
			outputStream.setPrivateKey(key);
			inputStream.setPrivateKey(key);
			outputStream.setClient(false);
			inputStream.setClient(false);
			friendName = inputStream.readLine();
			shell.setText(friendName);
			currentSocket = tempSocket; 
			new ChatListener(inputStream).start();
		} catch (Exception e) {
			e.printStackTrace();
		}
		shell.addDisposeListener(this);
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
	}
	
	private class ChatListener extends Thread
	{
		private BufferedReader inputStream;
		private String line;
		public ChatListener(BufferedReader is)
		{
			inputStream =is;
		}
		@Override
		public void run() {
			super.run();
			try {
				while(true)
				{
					
					line = inputStream.readLine();
					if(checkIfEndChat(line))
						break;
					
					ITalkServer.this.display.asyncExec(new Runnable(){
						@Override
						public void run() {
							if(!isRecieving)
							{
								chatWindow.append(friendName+ChatStrings.USER_NEXT+line+"\n");
								isRecieving = true;
							}else
								chatWindow.append(ChatStrings.SAPCES+line+"\n");
							if(line.toLowerCase().compareTo("buzz") == 0)
							{
								vibrate();
								AudioPlayer.player.start(getBuzzAudioStream());
								return;
							}
							AudioPlayer.player.start(getMessageAudioStream());
						}});
				}
			} catch (Exception e) {
				System.out.println("Partner has client chating");
			}
		}
	}
	
	@Override
	protected void handleSend(String line) {
		try {
			if(line == null || line.compareTo("") == 0)
				return;
			if(isRecieving)
			{
				chatWindow.append(loginName+ChatStrings.USER_NEXT+line+"\n");
				isRecieving = false;
			}
			else
				chatWindow.append(ChatStrings.SAPCES+line+"\n");
			///chatArea.setCaretPosition(chatArea.getText().length());
			
			outputStream.writeString(line+ChatStrings.END_LINE);	
		} catch (Exception e) {
			//e.printStackTrace();
			System.err.print("partner is closed");
		}	
	}

	@Override
	public void widgetDisposed(DisposeEvent e) {
		try {
			byte[] end = new byte[3];
			end[0] = 1;
			end[1] = 2;
			end[2] = 3;
			String bytes = new String(end);
			outputStream.writeString(bytes+ChatStrings.END_LINE);
		} catch (Exception e2) {
			e2.printStackTrace();
		}
		
	}

}
